package com.canaan.business.service.basic.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import com.canaan.business.domain.basic.WmsCodeRule;
import com.canaan.business.domain.basic.WmsCodeRuleDetail;
import com.canaan.business.domain.vo.WmsCodeRuleVo;
import com.canaan.business.mapper.basic.WmsCodeRuleMapper;
import com.canaan.business.service.basic.IWmsCodeRuleDetailService;
import com.canaan.business.service.basic.IWmsCodeRuleService;
import com.canaan.common.exception.ServiceException;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.StringUtils;
import com.canaan.common.utils.TableNoUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.canaan.common.utils.SecurityUtils.getUsername;

/**
 * 条码规则Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-29
 */
@Service
public class WmsCodeRuleServiceImpl implements IWmsCodeRuleService {
    @Resource
    private WmsCodeRuleMapper wmsCodeRuleMapper;
    @Resource
    private IWmsCodeRuleDetailService wmsCodeRuleDetailService;
    /**
     * 查询条码规则
     *
     * @param id 条码规则主键
     * @return 条码规则
     */
    @Override
    public WmsCodeRule selectWmsCodeRuleById(Long id) {
        return wmsCodeRuleMapper.selectWmsCodeRuleById(id);
    }

    /**
     * 查询条码规则列表
     *
     * @param wmsCodeRule 条码规则
     * @return 条码规则
     */
    @Override
    public List<WmsCodeRule> selectWmsCodeRuleList(WmsCodeRule wmsCodeRule) {
        return wmsCodeRuleMapper.selectWmsCodeRuleList(wmsCodeRule);
    }

    /**
     * 新增条码规则
     *
     * @param wmsCodeRule 条码规则
     * @return 结果
     */
    @Override
    public int insertWmsCodeRule(WmsCodeRule wmsCodeRule) {
        if (StringUtils.isEmpty(wmsCodeRule.getNo())){
            throw new ServiceException("编号不能为空");
        }
        wmsCodeRule.setCreateTime(DateUtils.getNowDate());
        wmsCodeRule.setCreateBy(getUsername());
        return wmsCodeRuleMapper.insertWmsCodeRule(wmsCodeRule);
    }

    /**
     * 修改条码规则
     *
     * @param wmsCodeRule 条码规则
     * @return 结果
     */
    @Override
    public int updateWmsCodeRule(WmsCodeRule wmsCodeRule) {
        wmsCodeRule.setUpdateTime(DateUtils.getNowDate());
        wmsCodeRule.setUpdateBy(getUsername());
        return wmsCodeRuleMapper.updateWmsCodeRule(wmsCodeRule);
    }

    /**
     * 批量删除条码规则
     *
     * @param ids 需要删除的条码规则主键
     * @return 结果
     */
    @Override
    public int deleteWmsCodeRuleByIds(Long[] ids) {
        return wmsCodeRuleMapper.deleteWmsCodeRuleByIds(ids);
    }

    /**
     * 删除条码规则信息
     *
     * @param id 条码规则主键
     * @return 结果
     */
    @Override
    public int deleteWmsCodeRuleById(Long id) {
        return wmsCodeRuleMapper.deleteWmsCodeRuleById(id);
    }

    @Override
    public WmsCodeRule selectWmsCodeRuleByNo(String ruleNo) {
        return wmsCodeRuleMapper.selectWmsCodeRuleByNo(ruleNo);
    }

    @Override
    @Transactional
    public int addAndUpdateDetail(WmsCodeRuleVo wmsCodeRuleVo) {
        List<WmsCodeRuleDetail> wmsCodeRuleDetailList = wmsCodeRuleVo.getWmsCodeRuleDetailList();
        WmsCodeRule wmsCodeRule = BeanUtil.toBean(wmsCodeRuleVo, WmsCodeRule.class);
        WmsCodeRule wmsCodeRuleByNo = this.selectWmsCodeRuleByNo(wmsCodeRuleVo.getNo());
        if (wmsCodeRuleVo.getId() == null) {
            if (wmsCodeRuleByNo != null) {
                throw new ServiceException("编号已存在");
            }
            this.insertWmsCodeRule(wmsCodeRule);
            if (CollUtil.isNotEmpty(wmsCodeRuleDetailList)) {
                wmsCodeRuleDetailList.forEach(e -> {
                    e.setRuleNo(wmsCodeRule.getNo());
                });
                wmsCodeRuleDetailService.batchInsertCodeRuleDetail(wmsCodeRuleDetailList);
            }
        } else {
            if (wmsCodeRuleByNo != null && wmsCodeRule.getId().longValue() != wmsCodeRuleByNo.getId().longValue()) {
                throw new ServiceException("编号已存在");
            }
            this.updateWmsCodeRule(wmsCodeRule);
            List<String> noList = wmsCodeRuleDetailList.stream().filter(e -> e.getNo() != null).map(e -> e.getNo()).collect(Collectors.toList());
            List<WmsCodeRuleDetail> wmsCodeRuleDetails = wmsCodeRuleDetailService.selectWmsCodeRuleDetailListByRuleNo(wmsCodeRule.getNo());

            List<WmsCodeRuleDetail> delList = wmsCodeRuleDetails.stream().filter(e -> !noList.contains(e.getNo())).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(delList)) {
                List<Long> ids = delList.stream().map(e -> e.getId()).collect(Collectors.toList());
                wmsCodeRuleDetailService.deleteWmsCodeRuleDetailByIds((ArrayUtil.toArray(ids, Long.class)));
            }

            List<WmsCodeRuleDetail> updateWmsCodeRuleDetail = wmsCodeRuleDetailList.stream().filter(e -> e.getId() != null).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(updateWmsCodeRuleDetail)) {
                wmsCodeRuleDetailService.batchUpdateCodeRuleDetail(updateWmsCodeRuleDetail);
            }

            List<WmsCodeRuleDetail> addWmsCodeRuleDetail = wmsCodeRuleDetailList.stream().filter(e -> e.getId() == null).collect(Collectors.toList());
            if (CollUtil.isNotEmpty(addWmsCodeRuleDetail)) {
                addWmsCodeRuleDetail.stream().forEach(e->{
                    e.setNo(TableNoUtil.getNo(WmsCodeRuleDetail.class));
                    e.setRuleNo(wmsCodeRule.getNo());
                });
                wmsCodeRuleDetailService.batchInsertCodeRuleDetail(addWmsCodeRuleDetail);
            }

        }
        return 1;
    }

    @Override
    public WmsCodeRuleVo getWithDetail(Long id) {
        WmsCodeRule wmsCodeRule = wmsCodeRuleMapper.selectWmsCodeRuleById(id);
        WmsCodeRuleVo wmsCodeRuleVo = BeanUtil.toBean(wmsCodeRule, WmsCodeRuleVo.class);
        List<WmsCodeRuleDetail> wmsCodeRuleDetails = wmsCodeRuleDetailService.selectWmsCodeRuleDetailListByRuleNo(wmsCodeRule.getNo());
        wmsCodeRuleVo.setWmsCodeRuleDetailList(wmsCodeRuleDetails);
        return wmsCodeRuleVo;
    }
}
